<template>
  <div>
    <el-upload
      :action="qiNiuUploadApi"
      list-type="picture-card"
      :headers="headers"
      :file-list="fileList"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-dialog v-model="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      dialogImageUrl: '456',
      dialogVisible: true,
      headers: { 'Authorization': getToken() },
      fileList: [],
      images: ''
    }
  },
  computed: {
    ...mapGetters([
      'qiNiuUploadApi',
      'baseApi'
    ])
  },
  methods: {
    handleRemove(file, fileList) {
      var s = ''
      fileList.forEach(function(item) {
        if (!item.raw && item.url.indexOf('blob') === -1) {
          s = s + item.url + ','
        } else {
          s = s + item.response.data[0] + ','
        }
      })

      this.images = s.substring(0, s.length - 1)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleSuccess(response, file, fileList) {
      console.log(this.images)
      var s = ''
      fileList.forEach(function(item) {
        if (!item.raw && item.url.indexOf('blob') === -1) {
          s = s + item.url + ','
        } else {
          s = s + item.response.data[0] + ','
        }
      })
      this.images = s.substring(0, s.length - 1)
    }
  }
}
</script>

<style>

</style>
